package root.electronclass.electronclass.school

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import retrofit2.Call
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentSchoolPropagandaBinding
import root.electronclass.electronclass.databinding.FragmentSchoolPropagandaItemBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.dp
import root.electronclass.electronclass.ext.tabs
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.CallbackNext
import root.electronclass.electronclass.mvp.entity.SchoolPropagandaDto
import root.electronclass.electronclass.mvp.entity.editSchoolPropagandaDto
import root.electronclass.electronclass.mvp.rest.RestManager
import root.electronclass.electronclass.other.VideoActivity
import root.electronclass.electronclass.school.model.SchoolPropagandaModel

class SchoolPropagandaFragment : ViewBindingFragment<FragmentSchoolPropagandaBinding>() {
    private val viewModel: SchoolPropagandaModel by viewModels()
    private var dataType = 1


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.load(page = 1)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val tabs = resources.getStringArray(R.array.class_tabs)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                getString(R.string.class_7_day) -> dataType = 1
                getString(R.string.class_3_month) -> dataType = 2
                getString(R.string.class_current_semester) -> dataType = 3
                getString(R.string.class_all) -> dataType = 4
            }

            viewModel.load(type = dataType, text = text, 1)
        }
        binding.recycler.addItemDecoration(GridSpacingItemDecoration(4, 36, false))
        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, page = 1)
            false
        }

        viewModel.data.observe(viewLifecycleOwner) {
            if (it.first == 1) {
                adapter.notifyChanged(it.second)
            } else {
                adapter.addAll(it.second)
            }
        }
    }

    private val adapter = BasicAdapter<SchoolPropagandaDto, FragmentSchoolPropagandaItemBinding>({ viewModel.load() }) { holder, item ->
        val lastItem = list.getOrNull(holder.adapterPosition - 1)
        val yyyyMMdd = item.onlineTime.take(10)
        val showHeader = lastItem?.onlineTime?.take(10) != yyyyMMdd
        holder.binding.itemTitle.isVisible = showHeader
        if (showHeader) {
            holder.binding.itemTitle.text = yyyyMMdd
        }
        holder.binding.itemTitle.text = item.name

        holder.binding.itemImage.setImageURI(item.pictureFormat?.getOrNull(0)?.url)
        holder.binding.itemAuthorTime.text = item.created + " | " + item.onlineTime
        holder.binding.itemStatus.let {
            it.isSelected = item.status == 2
            it.text = if (it.isSelected) getString(R.string.played) else getString(R.string.not_played)
        }
        holder.binding.itemLevel.text = when (item.videoType) {1 -> "专题片" else -> "形象宣传" }
        holder.binding.root.setOnClickListener {
            VideoActivity.startActivity(requireActivity(), item.videoUrlFormat.orEmpty()[0].url.orEmpty())
            item.playStatus = 1
            item.playNum++
            edit(item)
        }

    }


    private fun edit(item: SchoolPropagandaDto){
        RestManager.getInstance().initRest().editCampusPublicity(editSchoolPropagandaDto(item.id))
            .enqueue(object : CallbackNext<BaseResponse<Any>>(mDialogViewModel) {
                override fun onSuccessful(call: Call<BaseResponse<Any>>?, response: BaseResponse<Any>?) {

                }

                override fun onError(call: Call<BaseResponse<Any>>?, response: BaseResponse<Any>?) {

                }

            })
    }
}